//  我的-录取查询-录取结果页面
const baseUrl='https://gsdse.cn/api/KJRecruit';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: "",
    userId: '',
    departmentName: "#",
    departmentId: '',
    realName: "",
    cheering: "",
    passStatus:"4",
    firstWillingText:"显示错误！",
    secondWillingText:"显示错误！",
    haveFirst:false,
    haveSecond:false,
    userWillingAndStatus:{}//用户各志愿审核情况
  },

  //--------------------------------------------------------------------------
  //methods

  back() {
    wx.switchTab({
      url: '../mine/mine',
    })
  },

  //获取用户姓名
  selectUser(userId) {
    // console.log(userId)
    return new Promise((resolve, reject) => {
      var that = this
      wx.request({
        url: baseUrl+'/selectUser',
        method: 'POST',
        data: {
          userId: userId
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res.data.data)
          resolve({
            userName:res.data.data.userName,
            departmentId:res.data.data.departmentId
          })
        }
      })
    })
  },

  //通过部门ID获取部门名称
  getDepartmentName(departmentId) {
    const departmentArray = ["无", '秘书处', '网络研发部', "影视部", '动画部', "PC技术部", "新媒体运营部", 'UI设计部']
    return departmentArray[departmentId]
  },

  //获取部门寄语
  selectDepartment(departmentId) {
    return new Promise((resolve, reject) => {
      // console.log('开始调用')
      var that = this
      wx.request({
        url: baseUrl+'/selectDepartment',
        method: 'POST',
        data: {
          departmentId: departmentId,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          // console.log(res.data.data)
          resolve(res.data.data.congratulation)
        }
      })
    })
  },

  //获取用户录取情况
  selectUserWilling(userId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl+'/SelectUserWilling',
        method: 'POST',
        data: {
          userId: userId,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          resolve({
            firstWilling: res.data.data.firstWilling,
            secondWilling: res.data.data.secondWilling,
            firstStatus: res.data.data.firstStatus,
            secondStatus: res.data.data.secondStatus
          })
        }
      })
    })
  },

  //通过departmentName反推departmentId
  getDepartmentId(departmentName) {
    var departmentArray = ["无", '秘书处', '网络研发部', "影视部", '动画部', "PC技术部", "新媒体运营部", 'UI设计部']
    for (let i = 1; i <= departmentArray.length; i++) {
      if (departmentName === departmentArray[i]) {
        return i
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(option) {
    var that = this
    //获取用户姓名
    const userInfo = await that.selectUser(option.userId)
    const departmentId = this.getDepartmentId(option.departmentName)
    that.setData({
      realName: userInfo.userName,
      userId: option.userId,
      departmentName:option.departmentName,
      departmentId:departmentId,
      passStatus:option.passStatus
    })
    //获取用户录取情况
    var beAdmittedId = userInfo.departmentId
    if (beAdmittedId) {
      //通过部门ID获取部门名称
      const departmentName = that.getDepartmentName(beAdmittedId)
      //获取部门寄语
      const cheering = await that.selectDepartment(beAdmittedId)
      // console.log(cheering)
      that.setData({
        departmentName: departmentName,
        cheering: cheering,
        departmentId: beAdmittedId,
      })
    }
    if(this.data.passStatus==="5"){
      wx.showToast({
      title: '部门还在审核',
    })
    //查看各志愿审核状态
    const WillingAndStatus=await this.selectUserWilling(that.data.userId)
    console.log(WillingAndStatus)
    let firstDepartmentName=""
    let secondDepartmentName=""
    if(WillingAndStatus.firstWilling!=="0"){
          firstDepartmentName=this.getDepartmentName(WillingAndStatus.firstWilling)
          that.setData({
            haveFirst:true
          })
    }
    if(WillingAndStatus.secondWilling!=="0"){
          secondDepartmentName=this.getDepartmentName(WillingAndStatus.secondWilling)
          that.setData({
            haveSecond:true
          })
    }

      let firstText="显示错误"
      let secondText="显示错误"
      console.log(that.data.haveFirst)
      console.log(that.data.haveSecond)
      if(that.data.haveFirst){
        if(WillingAndStatus.firstStatus==="1") firstText="被录取";
        else if(WillingAndStatus.firstStatus==="0") firstText="未被录取";
        else if(WillingAndStatus.firstStatus===null) firstText="审核中";
        that.setData({
          firstWillingText:firstDepartmentName+":"+firstText
        })
      }
      if(that.data.haveSecond){
         if(WillingAndStatus.secondStatus==="0") secondText="未被录取";
        else if(WillingAndStatus.secondStatus===null) secondText="审核中";
        else if(WillingAndStatus.secondStatus==="1") secondText="被录取";
        that.setData({
          secondWillingText:secondDepartmentName+":"+secondText
        })
      }
      console.log(that.data.secondWillingText)
      console.log(that.data.firstWillingText)
      

    }
    console.log(typeof this.data.passStatus)
    console.log(this.data.passStatus)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})